package com.glsc.ngateway.common.base.repo.mysql.gateway.oaflow;

import com.glsc.ngateway.common.base.domain.mysql.gateway.oaflow.TOaFlowMetaTable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Repository
public interface TOaFlowMetaTableRepository extends JpaRepository<TOaFlowMetaTable, Long>, JpaSpecificationExecutor<TOaFlowMetaTable> {


/*
    @Transactional(readOnly = true)
    @Query(value = "select f from Fax")
    List<Fax> findAll();
*/

    @Query(value = "select f from TOaFlowMetaTable f where f.id = :id")
    TOaFlowMetaTable findExist(@Param("id") Long id);


    @Query(value = "select distinct new TOaFlowMetaTable(f.workFlowId, f.flowName) from TOaFlowMetaTable f",
            countQuery = "select count(distinct f.workFlowId) from TOaFlowMetaTable f")
    List<TOaFlowMetaTable> getAllFlow();

    @Query(value = "select distinct f.work_flow_id workFlowId, f.flow_name flowName " +
            "from t_oa_flow_meta_table f where " +
            "1=1 "+
            "and (:flowName is null or f.flow_name like CONCAT('%',:flowName,'%')) " +
            "order by flow_name asc ",
            countQuery = "select count(DISTINCT f.work_flow_id, f.flow_name) as cnt " +
                    "from t_oa_flow_meta_table f where " +
                    "1=1 "+
                    "and (:flowName is null or f.flow_name like CONCAT('%',:flowName,'%')) " +
                    ""
            ,nativeQuery = true )
    Page<Map<String, String>> queryFlow(Pageable pageable, String flowName);

    @Query(value = "select new TOaFlowMetaTable(f.id,f.flowFieldName,f.interfaceFieldName,f.fieldType,f.isRequired,f.isDoc,f.detailIndex,f.isPerson) from TOaFlowMetaTable f where f.workFlowId = :workFlowId")
    List<TOaFlowMetaTable> getById(String workFlowId);

//    @Transactional
//    @Modifying
//    @Query(value = "update FlowMetaData f set #{#key} = :value where f.workFlowId = :workFlowId")
//    FlowMetaData updateByWorkFlowId(@Param("workFlowId") String workFlowId, @Param("key") String key, @Param("value") String value);

//    @Transactional
//    @Modifying
//    @Query(value = "update FlowMetaData f set #{#key} = :value where f.workFlowId = :workFlowId and f.flowFieldName = :flowFieldName")
//    FlowMetaData updateFlowMetaData(@Param("workFlowId") String workFlowId, @Param("flowFieldName") String flowFieldName, @Param("key") String key, @Param("value") String value);


    @Transactional
    @Modifying
    @Query(value = "delete from TOaFlowMetaTable f where f.workFlowId = :workFlowId")
    int deleteByWorkFlowId(@Param("workFlowId") String workFlowId);

    @Transactional
    @Modifying
    @Query(value = "delete from TOaFlowMetaTable f where f.workFlowId = :workFlowId and f.interfaceFieldName = :interfaceFieldName")
    int deleteByFieldName(@Param("workFlowId") String workFlowId, @Param("interfaceFieldName") String interfaceFieldName);


    @Transactional
    @Modifying
    @Query(value = "delete from TOaFlowMetaTable f where f.id NOT IN (:ids) and f.workFlowId = :workFlowId")
    int deleteById(@Param("ids") List<Long> ids, @Param("workFlowId") String workFlowId);



    List<TOaFlowMetaTable> findByWorkFlowId(String workFlowId);

    /**
     * @param workFlowId
     * @return
     * @Description 查询流程必填字段interface_field_name
     */
    @Query(value = "select m.interfaceFieldName from TOaFlowMetaTable m where m.isRequired = 1 and m.workFlowId = :workFlowId and m.detailIndex = :detailIndex")
    Set<String> getMandatoryFields(String workFlowId, String detailIndex);


    @Query(value = "select COUNT(DISTINCT(m.detailIndex)) as mandatoryFields from TOaFlowMetaTable m where m.isRequired = 1 and m.workFlowId = :workFlowId and m.detailIndex != 0")
    String getMandatoryDetailFields(String workFlowId);


    /**
     * @param workFlowId
     * @return
     * @Description 查询流程元数据
     */
    @Query(value = "select m from TOaFlowMetaTable m where m.workFlowId = :workFlowId order by m.fieldType asc")
    List<TOaFlowMetaTable> getFlowMetaDataList(String workFlowId);

//    @Query(value = "select m.flowFieldName,m.interfaceFieldName,m.isPerson,m.fieldType,m.isDoc from TOaFlowMetaTable m where m.workFlowId = :workFlowId")
//    List<Map<String, String>> getFlowFieldMapList(String workFlowId);

}
